package com.deshan.websys.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deshan.websys.code.logger.Loggers;
import com.deshan.websys.code.mapper.SmsHomeAdvertiseMapper;
import com.deshan.websys.code.model.SmsHomeAdvertise;
import com.deshan.websys.code.service.SmsHomeAdvertiseService;
import com.deshan.websys.code.utils.BeanUtil;
import com.deshan.websys.code.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<SmsHomeAdvertiseMapper, SmsHomeAdvertise> implements SmsHomeAdvertiseService {
    @Resource
    SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    @Override
    public List<SmsHomeAdvertise> getAdvertises() {
        QueryWrapper<SmsHomeAdvertise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        List<SmsHomeAdvertise> resultDatalist = smsHomeAdvertiseMapper.selectList(queryWrapper);
        return resultDatalist;
    }

    @Override
    public int saveOrUpdate_new(SmsHomeAdvertise advertise) {
        int result = 0;
        String id = advertise.getId();
        if (StringUtils.isEmpty(id)) {
            result = smsHomeAdvertiseMapper.insert(advertise);
        } else {

            SmsHomeAdvertise desAdvertise = smsHomeAdvertiseMapper.selectById(id);
            try {
                BeanUtil.copyNotNullProperties(desAdvertise, advertise);
                result = smsHomeAdvertiseMapper.updateById(desAdvertise);
            } catch (Exception ex) {
                Loggers.serviceLogger.error(ex.getMessage());
            }
        }
        return result;
    }

    @Override
    public int delete(String id) {
        int result = 0;
        try {
            result = smsHomeAdvertiseMapper.deleteById(id);
        } catch (Exception ex) {

        }

        return result;
    }
}
